package com.ruoyi.base.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.lang.tree.Tree;
import com.ruoyi.base.domain.SyYj;
import com.ruoyi.base.domain.vo.SyYjDetailVo;
import com.ruoyi.common.core.domain.entity.SysDept;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckLogin;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.base.domain.vo.SyYjVo;
import com.ruoyi.base.domain.bo.SyYjBo;
import com.ruoyi.base.service.ISyYjService;

/**
 * 试验依据
 *
 * @author zyb
 * @date 2024-04-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/base/syYj")
public class SyYjController extends BaseController {

    private final ISyYjService iSyYjService;

    /**
     * 查询试验依据列表
     */
    @SaCheckLogin
    @GetMapping("/list")
    public R<List<SyYjVo>> list(SyYjBo bo) {
        List<SyYjVo> list = iSyYjService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出试验依据列表
     */
    @SaCheckLogin
    @Log(title = "试验依据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SyYjBo bo, HttpServletResponse response) {
        List<SyYjVo> list = iSyYjService.queryList(bo);
        ExcelUtil.exportExcel(list, "试验依据", SyYjVo.class, response);
    }

    /**
     * 获取试验依据详细信息
     *
     * @param syYjId 主键
     */
    @SaCheckLogin
    @GetMapping("/{syYjId}")
    public R<SyYjVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long syYjId) {
        return R.ok(iSyYjService.queryById(syYjId));
    }

    /**
     * 新增试验依据
     */
    @SaCheckLogin
    @Log(title = "试验依据", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SyYjBo bo) {
        return toAjax(iSyYjService.insertByBo(bo));
    }

    /**
     * 修改试验依据
     */
    @SaCheckLogin
    @Log(title = "试验依据", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SyYjBo bo) {
        return toAjax(iSyYjService.updateByBo(bo));
    }

    /**
     * 删除试验依据
     *
     * @param syYjIds 主键串
     */
    @SaCheckLogin
    @Log(title = "试验依据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{syYjIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] syYjIds) {
        return toAjax(iSyYjService.deleteWithValidByIds(Arrays.asList(syYjIds), true));
    }


    /**
     * 获取依据树列表
     */
    @SaCheckLogin
    @GetMapping("/yjTree")
    public R<List<Tree<Long>>> yjTree(SyYj yj) {
        return R.ok(iSyYjService.selectYjTreeList(yj));
    }

    /**
     * 获取依据子集/明细
     * @param yj
     * @param pageQuery
     * @return
     */
    @SaCheckLogin
    @GetMapping("/listByYjId")
    public R<SyYjDetailVo> listByYjId(SyYj yj, PageQuery pageQuery) {
        return R.ok(iSyYjService.listByYjId(yj, pageQuery));
    }

}
